package pro.shuangxi.config.mybatisplus;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

import javax.sql.DataSource;
@Configuration
@MapperScan("mybatisplus.mapper")
@MapperScan("pro.shuangxi.model.*.mapper")
@ComponentScan(value="mybatisplus",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})})
public class MybatisPlusRootConfig {
	@Autowired
	DataSource dataSource;
	@Bean
	public MybatisSqlSessionFactoryBean initSqlSession(){
		MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
		MybatisConfiguration configuration = new MybatisConfiguration();
		configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
		factoryBean.setConfiguration(configuration);
		factoryBean.setDataSource(dataSource);
		factoryBean.getConfiguration().addInterceptor(mybatisPlusInterceptor());
		return factoryBean;
	}
	@Bean
	public MybatisPlusInterceptor mybatisPlusInterceptor() {
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
		return interceptor;
	}

}
